import {
  Body,
  Controller,
  Get,
  Param,
  Post,
  Put,
  Query,
  Delete,
  Request,
} from '@nestjs/common';
import { QuestionDTO } from './dto/question.dto';
import { QuestionService } from './question.service';
import { DeleteResult } from 'mongoose';
import { Public } from 'src/auth/decorators/public.decorator';

@Controller('question')
export class QuestionController {
  constructor(private readonly questionService: QuestionService) {}

  // 获得单个问卷信息
  @Public()
  @Get(':id')
  getOne(@Param('id') id: string) {
    return this.questionService.findOne(id);
  }

  // 修改单个问卷
  @Put(':id')
  updateOne(
    @Param('id') id: string,
    @Body() questionDTO: QuestionDTO,
    @Request() req,
  ) {
    const { username } = req.user;
    return this.questionService.updateOne(id, questionDTO, username);
  }

  // 创建单个问卷
  @Post()
  createOne(@Body() questionDTO: QuestionDTO, @Request() req) {
    const { username } = req.user;
    return this.questionService.createOne(questionDTO, username);
  }

  // 获得所有问卷信息
  @Get()
  getAll(
    @Query('page') page: number = 1,
    @Query('keyword') keyword: string = '',
    @Query('pageSize') pageSize: number = 4,
    @Query('isStar') isStar: boolean = false,
    @Query('isDeleted') isDeleted: boolean = false,
    @Request() req,
  ) {
    const { username } = req.user;
    return this.questionService.find({
      page,
      keyword,
      pageSize,
      isStar,
      isDeleted,
      author: username,
    });
  }

  //删除单个问卷
  @Delete(':id')
  deleteOne(@Param('id') id: string, @Request() req) {
    const { username } = req.user;
    return this.questionService.deleteOne(id, username);
  }

  //批量删除
  @Delete()
  deleteMany(@Body() body, @Request() req): Promise<DeleteResult> {
    const { username } = req.user;
    const { ids = [] } = body;
    return this.questionService.deleteMany(ids, username);
  }

  // 复制问卷
  @Post('duplicate/:id')
  duplicate(@Param('id') id: string, @Request() req) {
    const { username } = req.user;
    return this.questionService.dulplicate(id, username);
  }
}
